package com.admin4j.server.auth;

import com.admin4j.framework.security.jwt.JwtUserDetails;
import com.admin4j.framework.security.jwt.JwtUserDetailsService;
import com.admin4j.server.system.convert.SysUserConvert;
import com.admin4j.server.system.entity.SysUser;
import com.admin4j.server.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author andanyang
 * @since 2023/11/7 14:28
 */
@Component
public class Admin4jJwtUserDetailsService implements JwtUserDetailsService, UserDetailsService {

    @Autowired
    ISysUserLoginInfoService sysUserLongInfoService;
    @Autowired
    ISysUserService sysUserService;

    @Override
    public JwtUserDetails loadUserByUserId(Long userId) {

        return sysUserLongInfoService.getUserInfoByUserId(userId);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByUserName(username);
        return SysUserConvert.INSTANCE.convert(sysUser);
    }
}
